package com.fz.controller;

import com.fz.entity.Student;
import com.fz.mapper.StudentMapper;
import com.fz.mybatis.MyHelper;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * Created by webrx on 2017-09-04.
 */
@Controller
@RequestMapping("/user")
public class BookController {
    @RequestMapping("/student")
    public String show(Model model){
        MyHelper mh = new MyHelper();
        SqlSession ss = mh.getSession();
        StudentMapper bdao = ss.getMapper(StudentMapper.class);
        model.addAttribute("stu",bdao.queryAll());
        return "/user/student";
    }

   @RequestMapping("/del/{id}")
    public String del(@PathVariable("id") int id){
        MyHelper mh = new MyHelper();
        SqlSession ss = mh.getSession();
       StudentMapper bdao = ss.getMapper(StudentMapper.class);
        bdao.deleteById(id);
        ss.commit();
        return "redirect:/user/student";  //重定向，
    }
    @RequestMapping("/insert")
    public String insert(Student stu){
        MyHelper mh = new MyHelper();
        SqlSession ss = mh.getSession();
        StudentMapper bdao = ss.getMapper(StudentMapper.class);
        bdao.insert(stu);
        ss.commit();
        return "redirect:/user/student";
    }
    @RequestMapping("/modify")
    public String modify(Student stu){
        MyHelper mh = new MyHelper();
        SqlSession ss = mh.getSession();
        StudentMapper bdao = ss.getMapper(StudentMapper.class);
        bdao.update(stu);
        ss.commit();
    return "redirect:/user/student";
    }
    @RequestMapping("/mod/{sid}")
    public String cha(Model model,@PathVariable("sid") int sid){
        MyHelper mhy = new MyHelper();
        SqlSession sss = mhy.getSession();
        StudentMapper dao = sss.getMapper(StudentMapper.class);
        model.addAttribute("st",dao.queryByid(sid));
        return "/user/show";
    }
}
